package me.ywy.skin.skincore;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;

import java.lang.reflect.Constructor;
import java.util.HashMap;
import java.util.Observable;
import java.util.Observer;

/**
 * Created by ywy on 2018-04-25.
 */
public class SkinLayoutFactory implements LayoutInflater.Factory2 , Observer {

    private static final String[] mClassPrefixList = {
            "android.widget.",
            "android.view.",
            "android.webkit."
    };
    private static final Class<?>[] mConstructorSignature = new Class[]{
            Context.class, AttributeSet.class};


    private static final HashMap<String, Constructor<? extends View>> sConstructorMap =
            new HashMap<String, Constructor<? extends View>>();

    //属性处理类
    private final SkilAttribute skinAttribute;

    public SkinLayoutFactory() {
        skinAttribute = new SkilAttribute();
    }

    @Override
    public View onCreateView(View parent, String name, Context context, AttributeSet attrs) {
        View view = createViewFromTag(name, context, attrs);
        if(null == view){
            view = creatView(name, context, attrs);
        }
        skinAttribute.load(view,attrs);

        return view;
    }

    private View createViewFromTag(String name, Context context, AttributeSet attrs) {
        //包含了. 自定义控件
        if(-1 != name.indexOf(".")){
            return null;
        }
        View view = null;
        for (int i = 0; i < mClassPrefixList.length; i++) {
            view = creatView(mClassPrefixList[i] + name, context, attrs);
            if(null != view){
                break;
            }
        }
        return view;

    }

    private View creatView(String name, Context context, AttributeSet attrs) {

        Constructor<? extends View> constructor = sConstructorMap.get(name);
        if(constructor == null){
            try {
                Class<? extends View> aClass = context.getClassLoader().loadClass(name).asSubclass(View.class);
                constructor = aClass.getConstructor(mConstructorSignature);
                sConstructorMap.put(name , constructor);
                return constructor.newInstance(context , attrs);
            } catch (Exception e) {
            }
        }

        if(constructor != null){
            try {
                return constructor.newInstance(context , attrs);
            } catch (Exception e) {
            }
        }

        return null;
    }

    @Override
    public View onCreateView(String name, Context context, AttributeSet attrs) {
        return null;
    }

    @Override
    public void update(Observable o, Object arg) {
        //更换皮肤
        skinAttribute.applySkin();
    }
}
